# 响应构建器类
from typing import Any, Optional, cast

from speech_convert.models.connect import ConnectionData
from speech_convert.models.response import ResponseCode
from speech_convert.models.speaker_info import SpeakerInfoData
from speech_convert.models.speech_recognition import SpeechRecognitionData
from speech_convert.utils.response_utils import ResponseUtils


class ResponseBuilder:
    """响应构建器，提供便捷的响应创建方法"""

    @staticmethod
    def success(
        message_id: str,
        timestamp: int,
        mode: str,
        status: str,
        text: str = "",
        session_id: str = "",
        start_time: Optional[int] = None,
        end_time: Optional[int] = None,
        duration: Optional[int] = None,
        confidence: float = 0.0,
        speaker_info: Optional[SpeakerInfoData] = None,
        message: str = "识别成功",
    ) -> SpeechRecognitionData:
        """创建语音识别成功响应"""
        data = SpeechRecognitionData(
            message_id=message_id,
            timestamp=timestamp,
            mode=mode,
            status=status,
            session_id=session_id,
            text=text,
            start_time=start_time,
            end_time=end_time,
            duration=duration,
            confidence=confidence,
            speaker_info=speaker_info,
        )
        return data

    @staticmethod
    def status(
        status: Optional[str] = None, details: Optional[dict[str, Any]] = None, message: str = "状态更新"
    ) -> "ResponseUtils[None]":
        """创建状态响应"""
        safe_status = status if status is not None else ""
        return cast(
            "ResponseUtils[None]", ResponseUtils.status_update(safe_status, details, message)
        )

    @staticmethod
    def error(
        code: int = ResponseCode.INTERNAL_ERROR, message: str = "语音识别失败"
    ) -> "ResponseUtils[None]":
        """创建语音识别错误响应"""
        return cast("ResponseUtils[None]", ResponseUtils.error(code, message))

    @staticmethod
    def connection(
        connection_id: str,
        server_info: dict[str, Any],
        supported_features: list[str],
        message: str = "连接成功",
    ) -> "ResponseUtils[ConnectionData]":
        """创建连接响应"""
        data = ConnectionData(
            connection_id=connection_id,
            server_info=server_info,
            supported_features=supported_features,
        )
        result = ResponseUtils.success(data, message)  # type: ignore
        return cast("ResponseUtils[ConnectionData]", result)
